<template>
  <van-list v-model="loading" :finished="finished" finished-text="没有更多了" :error.sync="error"
    error-text="请求失败，点击重新加载" @load="onLoad">
    <comment-item v-for="(item, index) in list" :key="index" :comment="item"/>
  </van-list>
</template>
<script>
import { getComments } from '@/api/comment';
import CommentItem from './comment-item.vue';
export default {
  name: 'CommentPage',
  components: {
    CommentItem
  },
  props: {
    source: {
      required: true,
    },
    list: {
      default: () => []
    }
  },
  data() {
    return {
      // list: [],
      loading: false,
      finished: false,
      offset: null,
      limit: 10,
      error: false,
    };
  },
  created() {},
  computed: {},
  methods: {
    onLoad() {
      // 1.请求获取数据
      getComments({
        type: 'a',
        source: this.source,
        offset: this.offset,
        limit: this.limit,
      })
        .then((res) => {
          console.log(res);
          // 2.赋值
          this.list.push(...res.results);
          this.$emit('onload-success', res.total_count);
          // 3.改变loading状态
          this.loading = false;
          // 4.判断是否结束
          if (res.results.length < 1) {
            // 将结束状态finished置为true
            this.finished = true;
          } else {
            this.offset = res.last_id;
          }
        })
        .catch((err) => {
          console.log(err);
          // 加载错误提示
          this.error = true;
          // 关闭loading
          this.loading = false;
        });
    },
  },
};
</script>
<style lang='less' scoped>
</style>
